package br.com.roteador.endereco;

import java.util.List;

import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import br.com.roteador.persistence.AbstractDAO;

@Named("destinoDAO")
public class EnderecoDAO extends AbstractDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4956297539946349174L;

	public EnderecoDAO (EntityManager manager){
		this.manager = manager;
	}

	public EnderecoDAO (){}

	public Endereco recuperaEnderecoPorCEPNumero(String cep, String numero) {
		CriteriaBuilder qb = manager.getCriteriaBuilder();
		CriteriaQuery<Endereco> cq = qb.createQuery(Endereco.class);
		Root<Endereco> tb = cq.from(Endereco.class);
		cq.select(tb);
		cq.where(qb.equal(tb.get("cep"), cep), qb.equal(tb.get("numero"), numero));
		return (Endereco) recuperaRegistro(montaQuery(cq));
	}

	public List<Endereco> recuperaEnderecos() {
		return (List<Endereco>) recuperaTodos(Endereco.class);
	}
	
}
